class Solution:
    def splitArray(self, nums: list, m: int) -> int:
        if len(nums) == m:
            return max(nums)
        min_rse, max_res = max(nums) - 1, sum(nums) + 1
        while max_res - min_rse > 1:
            mid_res = (min_rse + max_res) // 2
            count, sum_num = 0, 0
            for num in nums:
                sum_num += num
                if sum_num > mid_res:
                    count += 1
                    sum_num = num
            count += 1
            if count > m:
                min_rse = mid_res
            else:
                max_res = mid_res
        return max_res


a = Solution()
print(a.splitArray([7, 2, 5, 10, 8], 2))
print(a.splitArray([1, 2], 2))
print(a.splitArray([2, 3, 1, 2, 4, 3], 5))
